package me.shurufa.holder;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.shurufa.R;
import me.shurufa.activities.UserInfoActivity;
import me.shurufa.model.Digest;
import me.shurufa.model.User;
import me.shurufa.utils.Constants;
import me.shurufa.utils.Utils;
import me.shurufa.widget.CircleImageView;

/**
 * 纵向用户列表ViewHolder
 *
 * @FileName: me.shurufa.holder.MoreUserHolder.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-25 15:05
 */
public class MoreUserHolder extends BaseViewHolder{

    @Bind(R.id.user_avatar)
    public CircleImageView userAvatar;
    @Bind(R.id.user_name)
    public TextView userName;
    @Bind(R.id.user_layout)
    public RelativeLayout userLayout;
    @Bind(R.id.user_desc)
    public TextView userDesc;
    @Bind(R.id.user_points)
    public TextView userPoints;
    @Bind(R.id.user_digests)
    public TextView userDigests;


    private Context mContext;
    private User mUser;
    private final SpannableString descStr;
    private final SpannableString countPoints;
    private final SpannableString publishDigest;
    private final SpannableString points;
    private final SpannableString item;

    public MoreUserHolder(View itemView, Object parent) {
        super(itemView, parent);
        mContext = itemView.getContext();
        ButterKnife.bind(this, itemView);
        addOnClickListener(userLayout);
        descStr = new SpannableString(mContext.getString(R.string.biography));
        descStr.setSpan(new ForegroundColorSpan(Color.parseColor("#999999")),
                0, descStr.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        countPoints = new SpannableString(mContext.getString(R.string.count_points));
        publishDigest = new SpannableString(mContext.getString(R.string.publish_digest));
        points = new SpannableString(mContext.getString(R.string.points));
        item = new SpannableString(mContext.getString(R.string.item));
    }

    public void bind(User user) {
        if (user == null)
            return;
        mUser = user;
        if (!TextUtils.isEmpty(user.avatar))
            Glide.with(mContext).load(user.avatar).into(userAvatar);
        else
            userAvatar.setImageResource(R.drawable.default_avatar);
        if (!TextUtils.isEmpty(user.nickname))
            userName.setText(user.nickname);

        if (!TextUtils.isEmpty(user.description))
            userDesc.setText(TextUtils.concat(descStr,user.description),TextView.BufferType.SPANNABLE);
        else
            userDesc.setText(descStr, TextView.BufferType.SPANNABLE);
        //TODO 还有用户积分和书摘数量未设置

        SpannableString pointsNum = new SpannableString(String.valueOf(user.credits_amount));
        pointsNum.setSpan(new ForegroundColorSpan(Color.parseColor("#d70c17")),
                0, pointsNum.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        userPoints.setText(TextUtils.concat(countPoints,pointsNum,points));

        SpannableString digestNum = new SpannableString(String.valueOf(user.excerpt_total));
        digestNum.setSpan(new ForegroundColorSpan(Color.parseColor("#d70c17")),
                0, digestNum.length(),Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        userDigests.setText(TextUtils.concat(publishDigest,digestNum,item));

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.user_layout:
                Intent intent = new Intent(mContext, UserInfoActivity.class);
                intent.putExtra(Constants.EXTRA_USER_PARAM,mUser.toString());
                mContext.startActivity(intent);
                break;
        }
    }
}
